#!/usr/bin/env python

"""
Discover available WSN devices.

Copyright (C) Sarah Mount, 2008.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

Discover available WSN devices via USB, Bluetooth, etc. Current known
device classes are:
 * Lego NXT bricks.
 * TinyOS devices (MICA series, Telos / Tmote series, etc).
 * Dingo managed simulated networks.
 * Dingo managed Gumstix networks.
 * Dingo mixed-mode networks.

This script currently ONLY runs on UNIX platforms (tested on Ubuntu Gutsy).

Usage: dingo-discover.py [options]

Options:
  -h, --help    show this help message and exit
  -l, --list    list all known device types
  -d, --dingo   search for Dingo networks
  -t, --tinyos  search for TinyOS devices
  -n, --nxt     search for Lego NXT devices
  -a, --all     search for all known devices (default behaviour)
"""

__author__ = 'Sarah Mount <s.mount@wlv.ac.uk>'
__date__   = 'March 2008'

import copy, glob, os, string, sys
from Dingo.discover import *


def printResults(device_l):
    """Print user friendly device list.
    """
    def printRes(devices):
        for device in devices:
            for attribute in device:
                print attribute, '\t',
            print
    sep = '----------------------------------------------------------'
    print
    print sep
    print 'Reference\tDevice\t\t\tDescription'
    print sep
    printRes(device_l),
    print sep
    print
        

if __name__ == '__main__':
    from optparse import OptionParser
    parser = OptionParser()
    parser.add_option('-l', '--list', dest='list_known', default=False,
                      action='store_true',
                      help='list all known device types')
    parser.add_option('-d', '--dingo', dest='dingo', default=True,
                      action='store_true',
                      help='search for Dingo networks')
    parser.add_option('-t', '--tinyos', dest='tinyos', default=True,
                      action='store_true',
                      help='search for TinyOS devices')
    parser.add_option('-n', '--nxt', dest='nxt', default=True,
                      action='store_true',
                      help='search for Lego NXT devices')
    parser.add_option('-a', '--all', dest='all', default=True,
                      action='store_true',
                      help='search for all known devices (default behaviour)')
    (options, args) = parser.parse_args()

    if options.list_known:
        print """
Device classes known to Dingo:

	* Lego NXT bricks.
        * TinyOS devices (MICA series, Telos / Tmote series, etc).
	* Dingo managed simulated networks.
        * Dingo managed Gumstix networks.
        * Dingo mixed-mode networks.
                """
        sys.exit()

    lego = tinyos = dingo = []

    print
    if hasTinyOS and options.tinyos:
        print '*** Searching for TinyOS devices ...',
    	tinyos = discoverTinyOS()
        if len(tinyos) != 1:
            print 'found', len(tinyos), 'devices.'
        else:
            print 'found', len(tinyos), 'device.'
    if hasNXT and options.nxt:
        print '*** Searching for Lego NXT bricks (may take some time) ...',
    	lego = discoverNXT()
        if len(lego) != 1:
            print 'found', len(lego), 'bricks.'
        else:
            print 'found', len(lego), 'brick.'        
    if options.dingo:
        print '*** Searching for Dingo-managed networks ...'
        dingo = discoverDingo()
#        if len(dingo) != 1:
#            print 'found', len(dingo), 'networks.'
#        else:
#            print 'found', len(dingo), 'network.'
    printResults(lego + tinyos + dingo)
